<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	

	include_once('../sys/sys.php');
	include_once('jsss.php');
	header('Content-Type: text/html; charset=utf-8');
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	//示例URL
	//http://www.89890.com/comic/1625/
	
	
	$url = 'http://www.89890.com/comic/' . $tid . '/';
	
	//分离URL以及ID
	$id = $tid;
	//定义公用变量
	if (substr($url,-1)!="/")
		$url.="/";
	$url_pre = $url;
	$pic_url_pre = 'http://www.89890.com/comic/';
		
		
	//获取URL内容
//	$url = "1.htm";
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	//关键字 漫画_89890漫画网
	$name = explode('漫画_89890漫画网',$content);
	$name = $name[0];
	$name = explode('<title>',$name);
	$name = $name[1];
	
	//获取最后更新时间
	$dt = explode('更新：',$content);
	$dt = $dt[1];
	$dt = explode(' 备注：',$dt);
	$dt = strtotime(trim($dt[0]));
	
	//获取作者
	$author = explode('作者：',$content);
	$author = explode('">',$author[1]);
	$author = explode('</a>',$author[1]);
	$author = $author[0];

	//$author = '<a target="_blank" href="/author/2754.html">郑健和</a> 状态：<span class="STYLE1">漫画连载中 </span>';
	
	
	//获取简介
	$des = explode('</a>详细介绍:</div>',$content);
	$des = explode('<div class="ms_box1">',$des[1]);
	$des = explode('<div class="ms_box2">',$des[0]);
	$des = $des[1];
	//echo $des;
	
	
	//获取是否连载中
	$lz = explode('状态：<span class="STYLE1">', $content);
	$lz = explode(' </span>', $lz[1]);
	if ($lz[0] == '漫画连载中') {
		$lz = 1;
	}else $lz = 0;
	
	
	//获取缩略图URL
	$img = explode('<div class="w_c"><img src="', $content);
	$img = explode('" alt=""', $img[1]);
	$img = $img[0];
	//echo $img;exit;
	
	
	//获取URL列表
	$urls = explode('<table width="100%" border="0" cellpadding="3" cellspacing="1"><tr>',$content);
	$urls = $urls[1];
	$urls = explode('</TD></table>',$urls);
	$urls = $urls[0];
	$replacer = array('<TD width="20%" height=22 vAlign=middle>',' </TD>','target=_blank ','·');
	$replacerc = array('');
	$urls = str_replace($replacer,$replacerc,$urls);
	
	
	$match_str = "/<a href = \'(.*)\'>/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	
	//由于是逆序，需要倒装数组(链接以及每个链接的数量)
	$urln = array_reverse($urln);
	
	
	//插入分类信息
	//检查当前ID是否存在
	$where = 'AND id= ' . $id .'';
	$result = $DB->build_and_exec_query(
		array('select' => 'COUNT(*) as max','from'   => '89890tags','where'	=> "1 " . $where ,)
	);	
	//当已经存在的时候更新日期
	if ($result['max'] > 0) {
		
	}
	//当不存在时插入
	else{
		do_taginsert($id,$dt,$name,0);
	}
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => '89890tags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
	//构造采集相关内容
	//图片服务器数组
	$urlnc = array();
	//每集相关信息解析
	foreach ($urln as $k => $v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		//构造JS路径
		$v =  urlencode($v);
		$contentc = get_js_url($tid, $v);
		
		echo '处理当前JS链接:' . 'http://www.89890.com/comic/' . $tid . '/' . $v . '/index.js<br>';
		ob_flush();
		
		$match_str = "/var total=(.*);var volpic='(.*)';var tpf = (.*);var tpf2 = (.*);/isU";
		preg_match_all($match_str,$contentc,$contentcn);
		$urlnc[$k]['total'] = $contentcn[1][0];
		$urlnc[$k]['volpic'] = mb_convert_encoding($contentcn[2][0],"UTF-8","GB2312");
		$urlnc[$k]['tpf2'] = $contentcn[4][0];
		//获取该集算法编号
		$urlnc[$k]['vol'] = get_pic_cal(file_get_contents('http://www.89890.com/comic/' . $tid . '/' . $v . '/'));
		//入库
		do_photoinsert($tid,$urlnc[$k]['volpic'],$urlnc[$k]['total'],$urlnc[$k]['tpf2'],$urlnc[$k]['vol']);
	}
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('89890tags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urln),
		 ),
		 $where
	);
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( '89890tags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count,$tpf2,$vol){
		global $DB;
		$DB->do_insert( '89890photos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								   'tpf2'					=> $tpf2,
								   'vol'					=> $vol,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>